package com.yonyou.pmclouds.signin.processor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.dao.query.CustomQueryParam;
import com.yonyou.pmclouds.basecom.dao.query.QueryParamBuilder;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogJobVo;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogMaintain;
import com.yonyou.pmclouds.signin.entity.SignInVO;
import com.yonyou.pmclouds.signin.mapper.SignInMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 智能日志记录维护后置处理器，新建或更新都使用
 * 出错不要影响其他本来的流程
 * @author xiejianyu
 * 2019-09-23
 */
@Slf4j
@Component
public class SignInProjectLogPostProcess implements SingleProcessor<SignInVO> {

    private static ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private ProjectLogMaintain projectLogMaintain;
    @Autowired
    private SignInMapper mapper;

    @Override
    public void process(SignInVO vo, SignInVO orig) throws BusinessException {
        //如果使用标准的 SingleInsert SingleUpdate SingleDelete 模板进行操作的话，新建时orig 为null，更新或删除时orig不为null
        try {
            if (orig==null) {//目前签到只有新增
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(vo.getCreationtime());

                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                Date start = calendar.getTime();

                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                Date end = calendar.getTime();

                QueryParamBuilder queryParamBuilder = QueryParamBuilder.newBuilder();
                queryParamBuilder.addWithValueQueryParam("pkProject", "=", vo.getPkProject());
                queryParamBuilder.addWithValueQueryParam("creator", "=", vo.getCreator());
                queryParamBuilder.addWithValueQueryParam("creationtime", ">=", start);
                queryParamBuilder.addWithValueQueryParam("creationtime", "<=", end);
                List<CustomQueryParam> queryParams = queryParamBuilder.build();

                //查询结果包含本次插入
                int signCount = mapper.countQuery(queryParams);
                if (signCount>1){
                    return;
                }

                ProjectLogJobVo jobVo = new ProjectLogJobVo();//最终放入队列的任务对象
                jobVo.setPkTenant(vo.getPkTenant());
                jobVo.setPkProject(vo.getPkProject());
                jobVo.setLogDate(start);
                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_SIGN_IN);
                jobVo.setEventTime(vo.getCreationtime());
                jobVo.setUserId(vo.getCreator());
                //pmCloudCacheManager.addObjToBlockingQueue(ProjectLogJobVo.JOB_QUEUE_NAME, jobVo);
                projectLogMaintain.upsert(jobVo);
            }
        } catch (Exception e) {
            log.error("签到记录--智能日志处理出错，记录pk：{}", vo.getPkSignin());
            log.error("异常信息", e);
        }
    }
}
